﻿#include "station.h"
#include "QPainter"
#include "QStyleOption"
#include "QDebug"

Station::Station(QWidget *parent) : QWidget(parent)
{
    initParam();

    connect(&mTimer, &QTimer::timeout, this, &Station::refresh);

    mTimer.start(100);
}

void Station::setInterval(quint16 interval)
{
    mTimer.setInterval(interval);
}

void Station::setTitle(QString str)
{
    mTitleStr = str;
}

void Station::setStation(QString str)
{
    mStationStr = str;
}

void Station::setFontPixelSize(quint8 size)
{
    mFontPixelSize = size;
}

void Station::refresh()
{
    this->update();
}

void Station::paintEvent(QPaintEvent *event)
{
    QStyleOption tOpt;

    tOpt.initFrom(this);

    QPainter tPainter(this);
    style()->drawPrimitive(QStyle::PE_Widget, &tOpt, &tPainter, this);
    QWidget::paintEvent(event);

//    QPen tPen(mColor);
//    tPainter.setPen(tPen);

//    QFont tFont(mFontFace, 18, QFont::Bold);
//    tFont.setPixelSize(mFontPixelSize);

//    tPainter.setFont(tFont);
    drawTitle(&tPainter);

    drawStation(&tPainter);

}

void Station::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event)

    initParam();
}

void Station::initParam()
{
    quint16 tTitleWidth = (this->width() * mTitleWidthProportion) / 100;

    mTitleRect.setLeft(0);
    mTitleRect.setRight(tTitleWidth);
    mTitleRect.setTop(0);
    mTitleRect.setBottom(this->height());

    mStationRect.setLeft(mTitleRect.right());
    mStationRect.setRight(this->width());
    mStationRect.setTop(0);
    mStationRect.setBottom(this->height());
}

void Station::drawTitle(QPainter *pPainter)
{
    pPainter->drawText(mTitleRect, Qt::AlignCenter, mTitleStr);
}

void Station::drawStation(QPainter *pPainter)
{
    if(mStationStr.size() > 7)
    {
        QRect tTopRect, tBottomRect;
        tTopRect.setLeft(mStationRect.left());
        tTopRect.setRight(mStationRect.right());
        tTopRect.setTop(mStationRect.top());
        tTopRect.setBottom((this->height() >> 1) - (mRowSpace >> 1));

        tBottomRect.setLeft(mStationRect.left());
        tBottomRect.setRight(mStationRect.right());
        tBottomRect.setTop((this->height() >> 1) + (mRowSpace >> 1));
        tBottomRect.setBottom(this->height());

        QString tStrTop, tStrBottom;
        tStrTop = mStationStr.mid(0, 6);
        tStrBottom = mStationStr.mid(6, mStationStr.size());
        pPainter->drawText(tTopRect, Qt::AlignLeft | Qt::AlignBottom, tStrTop);
        pPainter->drawText(tBottomRect, Qt::AlignLeft | Qt::AlignTop, tStrBottom);
        return;
    }

    pPainter->drawText(mStationRect, Qt::AlignLeft | Qt::AlignVCenter, mStationStr);
}
